/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.server;

import java.util.Collections;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import net.sf.jsr107cache.CacheException;
import net.sf.jsr107cache.CacheManager;

import org.jb.sharedbills.shared.svc.EventService;
import org.jb.sharedbills.shared.svc.MessageService;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.inject.Provider;

/**
 * Substitut à Guice qui a un temps de demarrage trop important pour le cas
 * d'utilisation d'AppEngine
 * 
 * @author jeremie
 *
 */
public class BasicDI {

	public static EventService getEventService() {
		return new EventServiceImpl( getUserService(), getEntityManagerProvider(), getCache() );
	}

	@SuppressWarnings("unchecked")
	private static Map<Object, Object> getCache() {
		if (cache == null) {
			try {
				cache = (Map) CacheManager.getInstance().getCacheFactory().createCache( Collections.emptyMap() );
			} catch (CacheException e) {
				e.printStackTrace();
			}
		}
		return cache;
	}

	/**
	 * 
	 * @return
	 */
	private static Provider<EntityManager> getEntityManagerProvider() {
		return providerEntityManager;
	}

	/**
	 * 
	 * @return
	 */
	private static UserService getUserService() {
		if (userService == null)
			userService = UserServiceFactory.getUserService();
		return userService;
	}

	/**
	 * 
	 * @return
	 */
	public static MessageService getMessageService() {
		return new MessageServiceImpl(getEntityManagerProvider(), getCache(), getUserProvider() );
	}

	/**
	 * 
	 * @return
	 */
	private static Provider<User> getUserProvider() {
		return userProvider;
	}

	private static UserService userService = null;
	private static ProviderEntityManager providerEntityManager = new ProviderEntityManager();
	private static Map<Object,Object> cache = null;
	private static UserProvider userProvider = new UserProvider();
	
	/**
	 * 
	 * @author jeremie
	 *
	 */
	private static class ProviderEntityManager implements Provider<EntityManager> {

		@Override
		public EntityManager get() {
			return getFactory().createEntityManager();
		}
		
		private synchronized EntityManagerFactory getFactory() {
			if (factory == null) {
				factory = Persistence.createEntityManagerFactory("transactions-optional");
			}
			
			return factory;
		}
		
		private EntityManagerFactory factory = null;
	}
	
	/**
	 * 
	 * @author jeremie
	 *
	 */
	private static class UserProvider implements Provider<User> {

		@Override
		public User get() {
			return getUserService().getCurrentUser();
		}
		
	}
}
